﻿
@{
    ViewBag.Title = "GroupCommentTrailList";
}
<section class="content-header">
    <h1>
        优惠券管理
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li class="active">群空间内容列表</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                    <form id="search-form" class="form-inline">
                        <div class="form-group">
                            <label>券码</label>
                            <input type="text" class="form-control" name="CouponCode" id="CouponCode" />
                        </div>
                        <div class="form-group">
                            <label>状态</label>
                            <select id="CouponStatus" class="form-control" name="CouponStatus">
                                <option value="-1">全部</option>
                                <option value="0">未领取</option>
                                <option value="1">已领取</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>类型</label>
                            <select id="CouponType" class="form-control" name="CouponType">
                                <option value="-1">全部</option>
                                <option value="1">洗车</option>
                                <option value="2">礼品</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-success btn-sm" id="btnSearch">查找</button>
                            <button type="button" class="btn btn-success btn-sm" id="btnAdd">新增</button>
                        </div>

                        <h3 class="box-title"></h3>
                    </form>
                </div>
                <div class="box-body table-responsive no-padding">
                    <div class="box box-primary">
                        <div class="box-header">
                            <div class="h4">优惠券列表</div>
                        </div>
                        <div class="box-body table-responsive no-padding">
                            <table class="table table-bordered table-hover body-table-head">
                                <thead>
                                    <tr>
                                        <th>
                                            ID
                                        </th>
                                        <th>
                                            券码
                                        </th>
                                        <th>
                                            用户
                                        </th>
                                        <th>
                                            券面价格
                                        </th>
                                        <th>
                                            过期时间
                                        </th>
                                        <th>
                                            渠道
                                        </th>
                                        <th>
                                            折扣
                                        </th>
                                        <th>
                                            类别
                                        </th>
                                        <th>
                                            批次
                                        </th>
                                        <th>
                                            领取状态
                                        </th>
                                        <th>使用状态</th>
                                        <th>使用时间</th>
                                        <th>
                                            创建时间
                                        </th>
                                        <th class="t_c">
                                            操作
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="tbodylist"></tbody>

                            </table>
                            <div class="no-data" id="nodata" style="display:none;">
                                <div class="img-box"></div>
                                <div class="text">暂无数据</div>
                            </div>
                        </div>
                        <div class="box-footer clearfix ">
                            <ul id="pagination" class="pagination pagination-sm no-margin pull-right"></ul>
                        </div>
                        <!-- /.box-body -->
                        <!-- /.box -->
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="modal-edit" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">优惠券信息</h4>
                </div>
                <div id="modal-body" class="modal-body">

                </div>
                <div class="modal-footer">
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <div class="modal fade" id="modal-user" style="display: none;">
        <div class="modal-dialog" style="width:80%">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">分配用户</h4>
                </div>
                <div id="modal-user-body" class="modal-body">

                </div>
                <div class="modal-footer">
                    
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</section>
@section scriptsBottom{
    <script src="@Url.Content("~/lib/art-template/template-web.js")"></script>
    <script id="dataItemTpl" type="text/html">
        {{each rows}}
        <tr>
            <td class="title">
                {{$value.CouponID}}
            </td>
            <td class="title">
                {{$value.CouponCode}}
            </td>
            <td class="title">
                <a style="cursor:pointer;" onclick="userDetail({{$value.CardUserId}})">
                    {{$value.UserName}}
                </a>
            </td>
            <td>
                {{$value.CouponPrice}}
            </td>
            <td>
                {{$value.ExpirationDate}}
            </td>
            <td>
                {{$value.Channel}}
            </td>
            <td>
                {{$value.Discount}}
            </td>
            {{if $value.CouponType==1}}
            <td>
                洗车
            </td>
            {{else}}
            <td>
                礼品
            </td>
            {{/if}}
            <td>
                {{$value.Batch}}
            </td>
            {{if $value.CouponStatus==0}}
            <td>
                未领取
            </td>
            {{else}}
            <td>
                已领取
            </td>
            {{/if}}

            {{if $value.UseStatus==0}}
            <td>
                未使用
            </td>
            {{else if $value.UseStatus==1 }}
            <td>
                已使用
            </td>
            {{else}}
            <td>已过期</td>
            {{/if}}
            <td>
                {{$value.UsedDate}}
            </td>
            <td>
                {{$value.CreateDate}}
            </td>
            <td class="t_c">
                {{if $value.CouponStatus==0}}
                <button type="button" class="btn btn-info btn-xs" onclick="toUser({{$value.CouponID}})">分配</button>
                {{/if}}
                <button type="button" class="btn btn-info btn-xs" onclick="edit({{$value.CouponID}})">编辑</button>
                <button type="button" class="btn btn-info btn-xs" onclick="del({{$value.CouponID}})">删除</button>
            </td>
        </tr>
        {{/each}}
    </script>
    <script type="text/javascript">
        window.selCouponId = 0;//分配的优惠券ID
        $(document).ready(function () {
            //分类
            childList();
            $("#btnSearch").on("click", function () {
                pageIndex = 1;
                childList();
            })
            $("#btnAdd").on("click", function () {
                var url = '@Url.Action("CouponEidt", "Coupon")' ;
                $("#modal-body").load(url, function () {
                    $("#modal-edit").modal("show");
                });
            })
           
        });

        function edit(id) {
            var url = '@Url.Action("CouponEidt", "Coupon")' + "?id=" + id ;
            $("#modal-body").load(url, function () {
                $("#modal-edit").modal("show");
            });
        }
        //分配用户
        function toUser(couponId) {
            var url = '@Url.Action("CouponToUser", "Coupon")' + "?couponId=" + couponId ;
            $("#modal-user-body").load(url, function () {
                $("#modal-user").modal("show");
            });
        }
       
        function userDetail(userId) {
             var url = '@Url.Action("Detail", "User")' + "?userId=" + userId;
            window.open(url);
        }
        function del(id) {
             var url = '@Url.Action("CommentDelete", "Coupon")' + "?id=" + id ;
             BMCW.ajax({
                    url: url,
                    type: "POST",
                    dataType: "json",
                        success: function (data) {
                            if (data.status == 0) {
                                BMCW.alert(data.message);
                                setTimeout(function () {
                                    window.location.reload();
                                }, 2000);
                            } else {
                                BMCW.alert(data.message);
                            }
                        }
                    });
        }
        //页码
        var pageIndex = 1;
        //查询
        function childList() {
            var fdata = $("#search-form").serializeJson();
            var url = '@Url.Action("CouponList", "Coupon")' + "?page=" + pageIndex;
            //查询列表
            BMCW.ajax({
                url: url,
                type: 'POST',
                data:fdata,
                dataType: "json",
                success: function (dt) {
                    if (dt.total < 1) {
                        $("#nodata").show();
                        document.getElementById('tbodylist').innerHTML = "";
                        return;
                    }
                    $("#nodata").hide();
                    //模板渲染
                    var data = dt.rows;
                    var html = template('dataItemTpl', dt);
                    document.getElementById('tbodylist').innerHTML = html;
                    //分页显示
                    $('#pagination').twbsPagination("destroy");
                    $('#pagination').twbsPagination({
                        totalItems: dt.total,
                        visiblePages: 10,
                        pageSize: 10,
                        startPage: parseInt(pageIndex),
                        onPageClick: function (event, page) {
                            pageIndex = page;
                            childList();
                        }
                    });
                }
            });
        }
    </script>
}